<?php
class Catalogue_media extends Admin_Controller{
	public function __construct(){
		parent::__construct();
		$this->load->Model(array("Mcataloguemedia"));
	}
	
	public function index(){
		$this->breadcrumb->append_crumb(trans('breadcrumb_home'),admincp_url());
		$this->breadcrumb->append_crumb(trans('breadcrumb_catalogue_media'),base_url().'admincp/catalogue_media');
		if($this->input->is_ajax_request()){
            $output = $this->Mcataloguemedia->listCatalogue();
            $this->output->set_output(json_encode($output));
        }else{
            $data['template']="admincp/catalogue_media/view";
            $data['title_page']='<i class="icon20 i-list"></i> '.trans('title_catalogue_media');
			$this->load->view("admincp/template",$data);
        }
	}

	public function add(){
		$this->breadcrumb->append_crumb(trans('breadcrumb_home'),admincp_url());
		$this->breadcrumb->append_crumb(trans('breadcrumb_catalogue_media'),base_url().'admincp/catalogue_media');
		$this->breadcrumb->append_crumb(trans('breadcrumb_catalogue_media_add'),base_url().'admincp/catalogue_media/add');
		$data['template']="catalogue_media/add";
		$data['title_page']='<i class="icon20 i-plus-circle-2"></i> '.trans('title_catalogue_media_add');
		if($this->input->server('REQUEST_METHOD') == "POST"){
			$data_insert = array(
				'cata_name' => $this->input->post('cata_name'),
				'date_add' => date('Y-m-d',time()), 
			);
			$this->Mcataloguemedia->insert($data_insert);
			$this->session->set_flashdata(array("success"=>trans('message_create_successfully')));
			redirect(base_url()."admincp/catalogue_media");		
		}
		$this->load->view("admincp/template",$data);
	}

	public function edit(){
		$id = $this->uri->segment(5);
		$this->breadcrumb->append_crumb(trans('breadcrumb_home'),admincp_url());
		$this->breadcrumb->append_crumb(trans('breadcrumb_catalogue_media'),base_url().'admincp/catalogue_media');
		$this->breadcrumb->append_crumb(trans('breadcrumb_catalogue_media_edit'),base_url().'admincp/catalogue_media/edit/id/'.$id);
		$data['template']="catalogue_media/edit";
		$data['title_page']='<i class="icon20 i-pen-2"></i> '.trans('title_catalogue_media_edit');
		$data['info'] = $this->Mcataloguemedia->get_data($id);
		if($this->input->server('REQUEST_METHOD') == "POST"){
			$data_update = array(
				'cata_name' => $this->input->post('cata_name'),
				'date_edit' => date('Y-m-d',time()), 
			);
			$this->Mcataloguemedia->update($data_update,$id);
			$this->session->set_flashdata(array("success"=>trans('message_edit_successfully')));
			redirect(base_url()."admincp/catalogue_media");
		}
		$this->load->view("admincp/template",$data);
	}

	public function delete(){
		$id = $this->uri->segment(5);
		$this->Mcataloguemedia->delete($id);
		$this->session->set_flashdata(array("success"=>trans('message_delete_successfully')));
		redirect(base_url()."admincp/catalogue_media");
	}
	
	public function check_name($name,$cata_id){
		if($this->Mcataloguemedia->check_name($name, $cata_id) == FALSE){
			return FALSE;
		}else{
			return TRUE;
		}
	}
}